<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex()
    {

        $id = session('user_id');
        //查询关注的tid
        $str = DB::table('zheye_user_love')->where('uid',$id)->first();
        $rep =  str_replace(',','',$str->tid);
        $arr = DB::table('zheye_topic')->select('name')->get();
        $row = [];
        foreach ($arr as $k => $v) {

            foreach ($v as $ks => $vs) {
                 array_push($row,$vs);

            }
        }
        $len = strlen($rep);
        return view('/home/article',['row'=>$row,'len'=>$len,'aa'=>0]);
    }

    public function postTijiao(Request $request)
    {
        

        $ip = $_SERVER['REMOTE_ADDR'];
        $roww =  $request->except('_token');
        $time = time();
        $roww['time'] = $time;
        $roww['ip'] = $ip;
        $roww['uid'] = session('user_id');
        // $file = $roww['pic'];
        
         //检测上传文件
        if($request->hasFile('pic')){

            //随机名
            $name = md5(rand(10000,99999).time());
            //获取后缀名
            $suffix = $request->file('pic')->getClientOriginalExtension();

            $request->file('pic')->move('./image/contentimg/',$name.'.'.$suffix);


            unset($roww['pic']);
            $roww['pic'] = '/image/contentimg/'.$name.'.'.$suffix;

            // var_dump($roww['pic']);
        }   
        $res = DB::table('zheye_article')->insertGetId($roww);
        // $res = false;
        if ($res) {
            return redirect('article/index')->with('error','发表成功');

            // return view('/home/article',['row'=>$row,'len'=>$len,'aa'=>'发表成功']);
        } else {
            // echo 1212;
            return redirect('article/index')->with('error','发表失败');
        }
    }
  
}


